/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.example.zhpan.bannerviewpager.slice;

import com.example.zhpan.bannerviewpager.ResourceTable;
import com.zhpan.bannerview.Banner;
import com.zhpan.bannerview.Log;
import com.zhpan.bannerview.listener.OnBannerListener;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.bundle.AbilityInfo;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * ThreeAbilitySlice
 *
 * @since 2021-03-24
 */
public class ThreeAbilitySlice extends AbilitySlice implements OnBannerListener {
    private static final int TWOTY = 20;
    private static final int TWOFIVEFIVE = 255;
    private static final int ONEFIVEZERO = 150;
    private static final int EIGHTZEROZERO = 800;
    private static final int FIVEZERO = 50;
    private static final int ONEZEROZERO = 100;
    private static final int THREE = 3;
    private static final int TWO = 2;
    private static final int ONE = 1;
    private static final int FIVE = 5;
    private static final int THREEZEROZERO = 3000;
    private static final int SIXZERO = 60;
    private static final int ONEZEROZEROZERO = 1000;
    private DirectionalLayout myLayout = new DirectionalLayout(this);
    private DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(
        ComponentContainer.LayoutConfig.MATCH_PARENT,
        ComponentContainer.LayoutConfig.MATCH_PARENT);

    private Banner banner;
    private Button button;
    private Text text;

    /**
     * onStart
     *
     * @param intent 參數
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setDisplayOrientation(AbilityInfo.DisplayOrientation.PORTRAIT);
        layoutConfig.setMargins(TWOTY, TWOTY, TWOTY, TWOTY);
        layoutConfig.alignment = LayoutAlignment.HORIZONTAL_CENTER;
        myLayout.setLayoutConfig(layoutConfig);
        layoutConfig.width = ComponentContainer.LayoutConfig.MATCH_CONTENT;
        layoutConfig.height = ComponentContainer.LayoutConfig.MATCH_CONTENT;
        try {
            banner = new Banner(this);
        } catch (NotExistException e) {
            Log.debug(e.getMessage());
        } catch (WrongTypeException e) {
            Log.debug(e.getMessage());
        } catch (IOException e) {
            Log.debug(e.getMessage());
        }
        ShapeElement element = new ShapeElement();
        element.setShape(ShapeElement.RECTANGLE);
        element.setRgbColor(new RgbColor(TWOFIVEFIVE, TWOFIVEFIVE, TWOFIVEFIVE));
        myLayout.setBackground(element);
        banner.setLayoutConfig(layoutConfig);
        button = new Button(this);
        button.setText("Banner演示:");
        button.setTextSize(ONEFIVEZERO);
        button.setBackground(element);
        button.setLayoutConfig(layoutConfig);
        banner.setOnBannerListener(this);
        text = new Text(this);
        initView(text," Banner 是一种灵巧好用的广告轮播控件，现在的绝大数app(如淘宝，支付宝，知乎，bilibili）"
            + "都需要使用banner界面，Banner实现了循环播放多个广告图片和手动滑动循环等功能");
        Text text1 = new Text(this);
        initView(text1,"111111111111111111");
        Text text2 = new Text(this);
        initView(text2,"222222222222222222222222");
        Text text3 = new Text(this);
        initView(text3,"3333333333333333333333333333");
        Text text4 = new Text(this);
        initView(text4,"44444444444444444444444444444");
        Text text5 = new Text(this);
        initView(text5,"5555555555555555555555555555");
        List<String> title = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        init(title,list);
        init1(title,list);
        super.setUIContent(myLayout);
    }

    /**
     * init
     *
     * @param title 參數
     * @param list 參數
     */
    private void init(List<String> title,List<Integer> list) {
        title.add("标题1");
        title.add("园您星梦");
        title.add("拳击");
        title.add("红色法拉利大促销");
        title.add("边看边买");
        list.add(ResourceTable.Media_1);
        list.add(ResourceTable.Media_2);
        list.add(ResourceTable.Media_3);
        list.add(ResourceTable.Media_4);
        list.add(ResourceTable.Media_5);
    }

    /**
     * initView
     *
     * @param text1 參數
     * @param str 參數
     */
    private void initView(Text text1,String str) {
        text1.setText(str);
        text1.setTextSize(FIVEZERO);
        text1.setMaxTextLines(ONEZEROZERO);
        text1.setMultipleLine(true);
        text1.setTruncationMode(Text.TruncationMode.NONE);
        text1.setLayoutConfig(layoutConfig);
    }

    private void init1(List<String> title,List<Integer> list) {
        try {
            banner.setImages(list).setBannerTitles(title).setScaleType(THREE)
                .setDelayTime(THREEZEROZERO)
                .setBannerStyle(ONE)
                .setTitleTextSize(SIXZERO).start();
        } catch (NotExistException e) {
            Log.debug(e.getMessage());
        } catch (WrongTypeException e) {
            Log.debug(e.getMessage());
        } catch (IOException e) {
            Log.debug(e.getMessage());
        }
        DirectionalLayout.LayoutConfig layoutConfig1 = new DirectionalLayout.LayoutConfig(
            ComponentContainer.LayoutConfig.MATCH_PARENT,
            ONEZEROZEROZERO);
        banner.setLayoutConfig(layoutConfig1);
        myLayout.addComponent(button);
        myLayout.addComponent(text);
        myLayout.addComponent(banner);
    }

    /**
     * onActive
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onBannerClick(int position) {
        if (position == THREE) {
            present(new SecondAbilitySlice(), new Intent());
        }
    }
}
